import json
from urllib.request import ProxyHandler
from urllib.request import Request
from urllib.parse import urlencode
from urllib.request import urlopen
from urllib.request import build_opener
from urllib.request import install_opener
from urllib.error import URLError

import pymysql

from catch.read_file import read
from urllib.request import HTTPHandler
from catch.data_base import get_connect

#根据关键词搜索相关用户，并保存用户id
def search(keyword,page):
    url = 'https://m.weibo.cn/api/container/getIndex'
    parm = {}
    parm['containerid'] = '100103type=17&q=' + keyword
    parm['title'] = '相关用户'
    parm['extparam']='tuid=0'
    parm['luicode'] = '10000011'
    parm['lfid'] = '100103type=1&q='+keyword
    parm['type'] = 'all'
    parm['page']=page


    cookie = open('../file/cookies.txt', 'r')
    cook=cookie.readline()
    print(cook)

    head = {'Accept': 'application/json, text/plain, */*',
        # 'Accept-Encoding': 'gzip, deflate, sdch, br',
        # 'Accept-Language': 'zh-CN,zh;q=0.8',
        'Connection': 'keep-alive',
        'Host': 'm.weibo.cn',
        'Referer': 'https://m.weibo.cn/p/100103type%3D1%26q%3D%E9%9D%A2%E8%86%9C?type=all&queryVal=%E9%9D%A2%E8%86%9C&luicode=10000011&lfid=106003type%3D1&title=%E9%9D%A2%E8%86%9C',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest', 'Cookie': cook}


    try:
        search_url=url+'?'+urlencode(parm)
        print('url:',search_url)
        proxy=ProxyHandler({'http':read()})
        opener=build_opener(proxy,HTTPHandler)
        install_opener(opener)
        request=Request(url=search_url,headers=head)
        respon=urlopen(request).read().decode('utf-8')
        print('respon:',respon)
        user_arr=[]
        try:
            data=json.loads(respon)
            res_data=data['data']
            print(res_data)
            cards=res_data['cards']
            for card in cards:
                if 'card_group' in card:
                    card_group=card['card_group']
                    for group in card_group:
                        user=group['user']
                        user_id=user['id']
                        user_fans=user['followers_count']
                        user_name=user['screen_name']
                        print('user_id:%s;user_name:%s;fans:%s'%(user_id,user_name,user_fans))
                        user_arr.append(user_id)
        except Exception as e:
            print(e)

        print(user_arr)
        file=open('../file/user.txt','a')
        for i in user_arr:
            file.write(str(i)+'\n')

    except URLError as e:
        if hasattr(e,'code'):
            print(e.code)
        if hasattr(e,'reason'):
            print(e.reason)


if __name__=='__main__':
    search('优惠券',1)